{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "781bf96d-cac4-40c8-a84c-aca80ae9f5f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "37.73856163024902\n",
      "[42.79685020446777, 45.552968978881836, 37.79125213623047, 29.129743576049805, 29.628276824951172]\n",
      "[65.08898735046387, 44.693708419799805, 37.75477409362793, 28.679847717285156, 29.616117477416992]\n",
      "[44.92473602294922, 44.255971908569336, 38.18035125732422, 29.174327850341797, 29.19459342956543]\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python  \n",
    "# coding=utf-8  \n",
    "#本段代码实现树莓派智能小车的红外避障效果\n",
    "#代码使用的树莓派GPIO是用的BOARD编码方式。 \n",
    "import RPi.GPIO as GPIO\n",
    "#电机控制器\n",
    "from motor import Motor\n",
    "#红外距离传感器\n",
    "from infrared_sensor import InfraredSensor\n",
    "#超声波避障\n",
    "from adafruit.Adafruit import Adafruit\n",
    "\n",
    "import time\n",
    "import sys\n",
    "\n",
    "#电机控制类\n",
    "motor = Motor()\n",
    "#超声波\n",
    "adafruit = Adafruit()\n",
    "\n",
    "#采集距离\n",
    "def collect_dists(adafruit,is_opposite=False,step_angle=0.25):\n",
    "    dists = [0,0,0,0,0]\n",
    "    \n",
    "    #从右边开始采集\n",
    "    if is_opposite == False:\n",
    "        for i in range(5):\n",
    "            dists[i]=adafruit.angel_dist(step_angle*i)\n",
    "            time.sleep(0.1)\n",
    "    #从左边开始采集\n",
    "    else:\n",
    "        for i in range(4,-1,-1):\n",
    "            dists[i]=adafruit.angel_dist(step_angle*i)\n",
    "            time.sleep(0.1)\n",
    "    return dists\n",
    "    \n",
    "def setup():\n",
    "    GPIO.setwarnings(False)\n",
    "    GPIO.setmode(GPIO.BCM)       # Numbers GPIOs by physical location\n",
    "\n",
    "    #初始化电机\n",
    "    motor.init()\n",
    "    #初始化超声波\n",
    "    adafruit.init()\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    setup()\n",
    "\n",
    "    #启动电机\n",
    "    motor.start()\n",
    "\n",
    "    is_opposite = False\n",
    "    \n",
    "    #将超声波的舵机向前\n",
    "    print(adafruit.front())\n",
    "    \n",
    "    try:\n",
    "        while True:\n",
    "            #采集超声波距离\n",
    "            dists = collect_dists(adafruit,is_opposite)\n",
    "            #调转采集的方向\n",
    "            is_opposite = not is_opposite\n",
    "            \n",
    "            print(dists)\n",
    "            \n",
    "            # time.sleep(0.3)\n",
    "            \n",
    "            # motor.up(100,9)\n",
    "            # motor.down(50,0.3)\n",
    "            # motor.left(50,3)\n",
    "            # motor.right(50,9)\n",
    "            # motor.stop(2)\n",
    "    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.\n",
    "        GPIO.cleanup()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82ba3458-23fe-44a3-8e1c-b5dcf1e262dc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef0f24ae-692a-4f51-8dd6-6448e843e8d7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tensorflow_env",
   "language": "python",
   "name": "tensorflow_env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
